﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace OISTools
{
    public partial class ConvertSQLToPGSql : Form
    {
        public ConvertSQLToPGSql()
        {
            InitializeComponent();
        }


        //List<ReplaceReg> list1 = new List<ReplaceReg>();
        Dictionary<string, string> list1 = new Dictionary<string, string>();
        private void btnReplace_Click(object sender, EventArgs e)
        {
            string RawText = richTextBox1.Text;
            foreach (var r in list1)
            {
                Regex reg = new Regex(r.Key);
                RawText = reg.Replace(RawText, r.Value);
            }
            richTextBox2.Text = RawText;
        }

        private void ConvertSQLToPGSql_Load(object sender, EventArgs e)
        {

            list1.Add(@"ON \[PRIMARY\] TEXTIMAGE_ON \[PRIMARY\]", @"");
            list1.Add(@"ON \[PRIMARY\]", @"");
            list1.Add(@"\[dbo\].\[(\w*)\]", @"$1");
            list1.Add(@"\[(\w*)\]", @"$1");
            list1.Add(@"nvarchar", @"character varying");
            list1.Add(@"varchar", @"character varying");
            list1.Add(@"u_s100", @"character varying(100)");
            list1.Add(@"u_s10", @"character varying(10)");
            list1.Add(@"u_s1", @"character varying(1)");
            list1.Add(@"u_s20", @"character varying(20)");
            list1.Add(@"u_s256", @"character varying(256)");
            list1.Add(@"u_s30", @"character varying(30)");
            list1.Add(@"u_s60", @"character varying(60)");
            list1.Add(@"u_text", @"text");
            list1.Add(@"u_othercode", @"character varying(3)");
            list1.Add(@"u_money", @"decimal(15,4)");
            list1.Add(@"u_int", @"int");
            list1.Add(@"u_image", @"image");
            list1.Add(@"u_hid", @"character varying(15)");
            list1.Add(@"u_date_backup", @"character varying(8)");
            list1.Add(@"u_date", @"character varying(10)");
            list1.Add(@"u_compid", @"character varying(4)");
            list1.Add(@"GO\n", @";");
            list1.Add(@"int IDENTITY \(1, 1\)", "serial");

            list1.Add(@" ntext ", " text ");

            //主鍵

            list1.Add(@"ALTER TABLE (\w*).*\n.* (\w*) PRIMARY KEY  CLUSTERED \n.*\(([^)]*)\) WITH  FILLFACTOR = 90", @"ALTER TABLE $1 ADD CONSTRAINT $2 PRIMARY KEY ($3)");
            list1.Add(@"WITH NOCHECK", "");
            list1.Add(@"CLUSTERED", "");
            list1.Add(@" ASC", "");
        }
    }
    //public class ReplaceReg
    //{
    //    public string sourceReg { set; get; }
    //    public string targetReg { set; get; }
    //}
}
